<template>
  <div>
    <div class="search">
      <input v-model="keyword" class="search-input" type="text" placeholder="输入城市名或拼音" />
    </div>
    <!--  搜索下面的结果集  -->
    <div
      class="search-content"
      ref="search"
      v-show="keyword"
    >
      <ul>
        <li class="search-item border-buttom"
          v-for="(item,index) of list" :key="item.id"
          @click="handleCityClick(item.name)"
        >
            {{item.name}}
        </li>
        <!--   v-show="!list.length"  这里还是用计算属性  搜索结果的显示hasNoData -->
        <li class="search-item border-buttom" v-show="hasNoData">
            没有找到匹配数据
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
import { mapMutations } from 'vuex'
export default {
  name: 'CitySearch',
  props: {
    cities: Object
  },
  data () {
    return {
      keyword: '',
      list: [],
      //  节流函数使用的定时器
      timer: null
    }
  },
  computed: {
    hasNoData () {
      return !this.list.length
    }
  },
  watch: {
    keyword () {
      //   这是节流函数，节流使用...拼音索引
      //
      if (this.timer) {
        clearTimeout(this.timer)
      }
      if (!this.keyword) {
        this.list = []
        return
      }
      //  100毫秒执行一次
      this.timer = setTimeout(() => {
        const result = []
        for (let i in this.cities) {
          this.cities[i].forEach((value) => {
            //拼音索引
            if (value.spell.indexOf(this.keyword) > -1 || value.name.indexOf(this.keyword) > -1) {
              result.push(value)
            }
          })
        }
        this.list = result
      }, 100)
    }
  },
  methods: {
      handleCityClick (city) {
        // console.log('通过vuex改变city-调用 actions');
        // 通过action,间接调用mutations..actions中的 ctx=this.$store实例
        // this.$store.dispatch('changeCity_a',city)
        //  组件中直接调用mutations
        // this.$store.commit('changeCity',city);
        //  使用辅助函数导入mutations
        this.changeCity(city)
        //改变城市之后跳转页面。。页面之间的联动用路由
        this.$router.push('/')
      },
      //  把vuex的方法抄过来
      ...mapMutations(['changeCity'])

    // handleCityClick (city) {
    //   this.changeCity(city)
    //   this.$router.push('/')
    // },
    // ...mapMutations(['changeCity'])
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.search,{ mouseWheel: true, click: true, tap: true })
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .search
    box-sizing:border-box
    height :.72rem
    background:$bgColor
    padding:0 .2rem
    .search-input
      /*border-box 告诉浏览器去理解你设置的边框和内边距的值是包含在width内的。*/
      //  因为不同浏览器版本元素的宽高，有的是包含边框和边距有的是不包含边框和边距的
      box-sizing border-box
      height:.62rem;
      line-height :.62rem
      width :100%
      text-align :center
      color #666
      padding:0 0.2rem

  .search-content

    border:.02rem solid #666;
    position absolute
    overflow hidden
    top 1.58rem
    left:0
    right:0
    buttom 0
    //  还是需要一个高度，要不然搞不动
    height:85%
    /*  这里加上背景色就会覆盖上一层的东西，不加就不会覆盖 */
    background #ccc
    /*  设置显示的优先级 */
    z-index:1
    .search-item
      line-height: .62rem
      padding-left: .2rem
      background:#fff
      color:#666
      border-bottom:.01rem solid #ccc;
//      这个貌似没用
/*    .border-buttom
      &:before
        border:.01rem solid green;
      &:after
        border:.01rem solid green;*/


  /*
  .search
    height: .72rem
    padding: 0 .1rem
    background: $bgColor
    .search-input
      box-sizing: border-box
      width: 100%
      height: .62rem
      padding: 0 .1rem
      line-height: .62rem
      text-align: center
      border-radius: .06rem
      color: #666
  .search-content
    z-index: 1
    overflow: hidden
    position: absolute
    top: 1.58rem
    left: 0
    right: 0
    bottom: 0
    background: #eee
    .search-item
      line-height: .62rem
      padding-left: .2rem
      background: #fff
      color: #666
   */
</style>
